# -*- coding:utf-8 -*-


import time
import json
import redis
import config
import pymongo
from scrapy import Spider
from scrapy.http import Request


class BitfinexExchange(Spider):
    name = 'bitfinex'

    handle_httpstatus_list = [429]

    def __init__(self, name=None, **kwargs):
        super(BitfinexExchange, self).__init__(name, **kwargs)

        # 连接mongodb
        client = pymongo.MongoClient('47.254.26.182', 27017)
        # client = pymongo.MongoClient('localhost', 27017)
        self.db = client.bt_news
        pairs = self.db.bt_transaction_pair.find({'exchange_name': 'bitfinex'})

        # 连接rdis
        self.redis_db = kwargs.get('redis_db') if 'redis_db' in kwargs else redis.StrictRedis(**config.redis_config)
        # 把URL存到reids中，这里使用的是reids的列表操作
        for pair in pairs:
            bitfinex_pair = pair.get('pair_name').replace('_', '').lower()
            # https://api.bitfinex.com/v1/pubticker/btcusd
            bitfinex_url = 'https://api.bitfinex.com/v1/pubticker/%s' % bitfinex_pair

            # 把这些URL放到缓存数据库redis中
            self.redis_db.rpush('bitfinex_url', bitfinex_url)

    def start_requests(self):
        url = self.redis_db.rpop('bitfinex_url').decode('utf8')

        yield Request(
            url=url,
            dont_filter=True,
            headers={
                'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
            },
            meta={

            },
            callback=self.exchange_info
        )
        # 再删除取出的URL，之后再插入redis,循环取URL，保证每个URL每秒都被执行。
        self.redis_db.lpush('bitfinex_url', url)

    def exchange_info(self, response):
        # 获取json中的数据，对应交易所信息
        body = json.loads(response.body)

        bitfinex_url = response.url
        # 交易对
        exchange_pair = bitfinex_url.split('/')[-1]
        # pair = exchange_pair[0:3]
        pair_name = exchange_pair[0:3]
        variual_pair = exchange_pair[3:6]

        # 交易所名称
        exchange_name = 'bitfinex'

        # 买家
        buy = body.get('ask')

        # 卖价
        sell = body.get('bid')

        # 最高价
        high = body.get('high')

        # 最低价
        low = body.get('low')

        # 成交量
        vol = body.get('volume')

        # 时间
        okcoin_time = body.get('timestamp')

        # 最新成交价
        last = body.get('last_price')

        # 入库mongo
        data = {
            'exchange_name': exchange_name,
            'variual_coin_name': pair_name,
            'variual_pair': variual_pair,
            # 'market_value': market_value,
            'buy': buy,
            'sell': sell,
            'high': high,
            'low': low,
            'last': last,
            'vol': vol,
            'time': okcoin_time,
            # 'roportion': roportion,
            # 'hasklin': hasklin,
            # 'change1d': change1d,
            'updatetime': time.localtime()
        }

        if response.status == 200:
            # 入库mongo
            self.db.bt_coin.update_one({'exchange_name': exchange_name, 'variual_coin_name': pair_name, 'variual_pair': variual_pair},
                                       {'$set': data}, upsert=True)


        # 循环更新数据
        url = self.redis_db.rpop('bitfinex_url').decode('utf8')
        yield Request(
            url=url,
            dont_filter=True,
            headers={
                'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36',
            },
            meta={

            },
            callback=self.exchange_info
        )
        self.redis_db.lpush('bitfinex_url', url)


'''
每三秒更新mongo中的数据
'''